home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / ghost / gs403src_png.lha / gs4.03 / libpng / pngrio.c < prev    next >
C/C++ Source or Header  |  1996-06-05  |  5KB  |  142 lines

  1.  
  2. /* pngrio.c - functions for data input
  3.  
  4.    libpng 1.0 beta 3 - version 0.89
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    May 25, 1996
  8.  
  9.    This file provides a location for all input.  Users which need
  10.    special handling are expected to write a function which has the same
  11.    arguments as this, and perform a similar function, but possibly has
  12.    a different input method.  Note that you shouldn't change this
  13.    function, but rather write a replacement function and then make
  14.    libpng use it at run time with png_set_read_fn(...) */
  15.  
  16. #define PNG_INTERNAL
  17. #include "png.h"
  18.  
  19. /* Read the data from whatever input you are using.  The default routine
  20.    reads from a file pointer.  Note that this routine sometimes gets called
  21.    with very small lengths, so you should implement some kind of simple
  22.    buffering if you are using unbuffered reads.  This should never be asked
  23.    to read more then 64K on a 16 bit machine.  The cast to png_size_t is
  24.    there to quiet some compilers */
  25. void
  26. png_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
  27. {
  28.    if (png_ptr->read_data_fn)
  29.       (*(png_ptr->read_data_fn))(png_ptr, data, length);
  30.    else
  31.       png_error(png_ptr, "Call to NULL read function");
  32. }
  33.  
  34. /* This is the function which does the actual reading of data.  If you are
  35.    not reading from a standard C stream, you should create a replacement
  36.    read_data function and use it at run time with png_set_read_fn(), rather
  37.    than changing the library. */
  38. #ifndef USE_FAR_KEYWORD
  39. static void
  40. png_default_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
  41. {
  42.    png_uint_32 check;
  43.  
  44.    check = fread(data, 1, (size_t)length, (FILE *)png_ptr->io_ptr);
  45.    if (check != length)
  46.    {
  47.       png_error(png_ptr, "Read Error");
  48.    }
  49. }
  50. #else
  51. /* this is the model-independent version. Since the standard I/O library
  52.    can't handle far buffers in the medium and small models, we have to copy
  53.    the data.
  54. */
  55.  
  56. #define NEAR_BUF_SIZE 1024
  57. #define MIN(a,b) (a <= b ? a : b)
  58.  
  59. #ifdef _MSC_VER
  60. /* for FP_OFF */
  61. #include <dos.h>
  62. #endif
  63.  
  64. static void
  65. png_default_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
  66. {
  67.    png_uint_32 check;
  68.    png_byte *n_data;
  69.  
  70.    /* Check if data really is near. If so, use usual code. */
  71. #ifdef _MSC_VER
  72.    /* do it this way just to quiet warning */
  73.    FP_OFF(n_data) = FP_OFF(data);
  74.    if (FP_SEG(n_data) == FP_SEG(data))
  75. #else
  76.    /* this works in MSC also but with lost segment warning */
  77.    n_data = (png_byte *)data;
  78.    if ((png_bytep)n_data == data)
  79. #endif
  80.    {
  81.       check = fread(n_data, 1, (size_t)length, (FILE *)png_ptr->io_ptr);
  82.    }
  83.    else
  84.    {
  85.       png_byte buf[NEAR_BUF_SIZE];
  86.       png_size_t read, remaining, err;
  87.       check = 0;
  88.       remaining = (png_size_t)length;
  89.       do
  90.       {
  91.          read = MIN(NEAR_BUF_SIZE, remaining);
  92.          err = fread(buf, 1, read, (FILE *)png_ptr->io_ptr);
  93.          png_memcpy(data, buf, read); /* copy far buffer to near buffer */
  94.          if(err != read)
  95.             break;
  96.          else
  97.             check += err;
  98.          data += read;
  99.          remaining -= read;
  100.       }
  101.       while (remaining != 0);
  102.    }
  103.    if (check != length)
  104.    {
  105.       png_error(png_ptr, "read Error");
  106.    }
  107. }
  108. #endif
  109.  
  110. /* This function allows the application to supply a new input function
  111.    for libpng if standard C streams aren't being used.
  112.  
  113.    This function takes as its arguments:
  114.    png_ptr      - pointer to a png input data structure
  115.    io_ptr       - pointer to user supplied structure containing info about
  116.                   the input functions.  May be NULL.
  117.    read_data_fn - pointer to a new input function which takes as it's
  118.                   arguments a pointer to a png_struct, a pointer to
  119.                   a location where input data can be stored, and a 32-bit
  120.                   unsigned int which is the number of bytes to be read.
  121.                   To exit and output any fatal error messages the new write
  122.                   function should call png_error(png_ptr, "Error msg"). */
  123. void
  124. png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
  125.    png_rw_ptr read_data_fn)
  126. {
  127.    png_ptr->io_ptr = io_ptr;
  128.  
  129.    if (read_data_fn)
  130.       png_ptr->read_data_fn = read_data_fn;
  131.    else
  132.       png_ptr->read_data_fn = png_default_read_data;
  133.  
  134.    /* It is an error to write to a read device */
  135.    png_ptr->write_data_fn = NULL;
  136.  
  137. #if defined(PNG_WRITE_FLUSH_SUPPORTED)
  138.    png_ptr->output_flush_fn = NULL;
  139. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  140. }
  141.  
  142.